package com.yanqu.road.server.cmd.activity.lottery;

import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ActivityLotteryProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.lottery.ActivityLotteryActivity;
import com.yanqu.road.server.manager.activity.lottery.CrossActivityLotteryMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = CrossProtocol.C_ACTIVITY_LOTTERY_DRAW_HIDE_BOX_EX, desc = "抽隐藏盲盒")
public class CrossActivityLotteryDrawHideBoxExCmd extends CrossCommand {

    int activityId;
    long userId;
    List<Integer> drawIsHide;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ActivityLotteryProto.CrossActivityLotteryDrawReqMsg reqMsg = ActivityLotteryProto.CrossActivityLotteryDrawReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        userId = packet.getPlayerId();
        drawIsHide = reqMsg.getDrawIsHideList();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        ActivityLotteryActivity activity = CrossActivityLotteryMgr.getLotteryActivity(activityId);
        if (activity == null) {
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }

        List<Integer> resultList = new ArrayList<>(drawIsHide);
        for (int i = 0; i < drawIsHide.size(); i++) {
            if (drawIsHide.get(i) == 1) {
                if (!activity.drawHideBox(userId)) {
                    resultList.set(i, 0);
                }
            }
        }
        drawIsHide = resultList;
    }

    @Override
    public void sendData() {
        ActivityLotteryProto.CrossActivityLotteryDrawRespMsg.Builder builder = ActivityLotteryProto.CrossActivityLotteryDrawRespMsg.newBuilder();
        builder.setRet(ret);
        builder.setActivityId(activityId);
        if (ret == 0) {
            builder.addAllDrawIsHide(drawIsHide);
        }
        MessageHelper.sendPacket(serverId, userId, YanQuMessageUtils.buildMessage(GameProtocol.S_ACTIVITY_LOTTERY_DRAW_HIDE_BOX_EX_FROM_CROSS, builder));
    }
}
